/****************************************************
    ScriptName:        Boss3.cs
    Author:            摸鱼程序男
    CreatTime:    2020/12/26 11:51:51
    Description:     Nothing
*****************************************************/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
using DG.Tweening;
using System.Linq;
using BTAI;
using ZYKTool;
using ZYKTool.Timer;
using LongZhiJie;

namespace HQL
{

    public class Boss3 : BossBase
    {
        public GameObject fire横;
        public GameObject fire竖;
        public GameObject 子弹;
        private int _damage;
        private float _speed;
        private void Awake()
        {
           
        }
        public override void Init()
        {

            base.Init();
            _aiRoot.OpenBranch(

                BT.If(CanAttack).OpenBranch(
                    BT.Call(Attack)
                ),
                 BT.If(CanWalk).OpenBranch(
           BT.Call(Walk)
                   )
            /*
           BT.While(die1).OpenBranch(
                 BT.Wait(1),
                 BT.Call(换阶段)
                  )
            */
            );
            _AttackTimer = 3;
            _SR = transform.GetOrAddComponent<SpriteRenderer>();
            _MyColor = _SR.color;
            _Blood = TapTapStaticValue.BossAttributes[BossName.Boss3].Blood;
            BossName = BossName.Boss3;
            _damage = 2;
            _speed = 60;
    }
        private bool CanWalk()
        {
            return _AttackOver;
        }
        public override void BossHunt(float damage)
        {
            TapTapStaticValue.TapTapModelManger.Damage(BossName.Boss3, damage);
            _Blood = TapTapStaticValue.BossAttributes[BossName.Boss3].Blood;
            if (TapTapStaticValue.BossAttributes[BossName.Boss3].Blood <= 0)
            {
                PlaySound(2);
                Die(BossName.Boss3);
                fire横.SetActive(false);
                fire竖.SetActive(false);
                TapTapStaticValue.TapTap升级系统.TapTapStartUpdate();
            }
           
        }

        protected override void Attack()
        {
            if (UnityEngine.Random.Range(0, 15) < 5)
            {
                switch (当前阶段)
                {
                    case 1:
                        Attack1();
                        break;

                }
            }
            else if (UnityEngine.Random.Range(0, 15) < 10 && UnityEngine.Random.Range(0, 15) >= 5)
            {
                
                Attack2();
            }
            else {
                Attack3();

            }
        }
        protected void Walk() 
        {
            //transform.position = Vector3.MoveTowards(transform.position, new Vector3(TapTapStaticValue.Player.transform.position.x, 0, 0), 0.1f);
            transform.position = Vector3.Lerp(transform.position, new Vector3(TapTapStaticValue.Player.transform.position.x, transform.position.y, transform.position.z), 0.5f * Time.deltaTime);
        }
        protected void Attack1() 
        {

            _SR.color = Color.blue;
            
                ZYKTool.Timer.ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask(
                   () =>
                   {
                       if (_已经死亡) return;

                       Shoot2(_damage, _speed);
                       Firework();
                   }, 2f, 1);
            
           
            
        }
        protected void Firework()
        {
            PlaySound();
            GameObject fire = null;
            if(UnityEngine.Random.Range(0,10)>5)
            {
                fire = fire横;
            }
            else
            {
                fire = fire竖;
            }
            fire.SetActive(true);
           
            ZYKTool.Timer.ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask(
                () =>
                {
                    if (_已经死亡) return;
                    _SR.color = _MyColor;
                    _AttackOver = true;
                    fire.SetActive(false);
                }, 2, 1);            
        }
        protected void Attack2() 
        {
            _SR.color = Color.blue;

            ZYKTool.Timer.ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask(
              () =>
              {
                  if (_已经死亡) return;


                  dash();
                  _AttackOver = true;
              }, 1f, 1);

        }
        protected void Attack3() 
        {
            _SR.color = Color.blue;

            int damage = UnityEngine.Random.Range(3, 6);
            ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                (() =>
                {
                    if (_已经死亡) return;
                    Shoot(damage > 4 ? 2 : 1, 40);
                },
                   damage, 1, PETimeUnit.Second);//三到五秒才射击

        }
        protected void dash()     
        {
            if (TapTapStaticValue.Player.transform.position.y <= 6)
            {
                if (_已经死亡) return;
                if (UnityEngine.Random.Range(0, 10) < 5)
                {

                    transform.DOKill();
                    // Debug.LogError("dash");
                    transform.DOMove(new Vector3(-55, transform.position.y, transform.position.z), 0.6f);
                }
                else
                {

                    transform.DOKill();
                    // Debug.LogError("dash");
                    transform.DOMove(new Vector3(64f, transform.position.y, transform.position.z), 0.6f);
                }
            }
            else {

                return;
            }
           
        }
        protected void Shoot(int damage, float speed)
        {
            if (_已经死亡) return;
            //Debug.LogError("shot");


            PlaySound();
            if (UnityEngine.Random.Range(0, 10) < 7)
            {
                int j = 0;
                //Vector3 dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
                for (int i = 0; i < 6; i++, j++)//散弹枪
                {
                    // Debug.LogError(transform.rotation);

                    Vector3 dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
                    GameObject bullet = Instantiate(子弹, transform.position, Quaternion.Euler(dir));
                    BossBullet bossbullet = bullet.GetOrAddComponent<BossBullet>();
                    bossbullet.Damage = damage;
                    bossbullet.speed = speed;
                    bossbullet.dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
                    bullet.transform.Rotate(new Vector3(0, 0, j * 20));
                    _AttackOver = true;
                    _SR.color = _MyColor;
                }
            }
            else 
            {
                int j = 0;
                for (int i = 0; i < 20; i++, j++)//散弹枪
                {
                    // Debug.LogError(transform.rotation);

                    Vector3 dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
                    GameObject bullet = Instantiate(子弹, transform.position, Quaternion.Euler(dir));
                    BossBullet bossbullet = bullet.GetOrAddComponent<BossBullet>();
                    bossbullet.Damage = damage;
                    bossbullet.speed = speed;
                    bossbullet.dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
                    bullet.transform.Rotate(new Vector3(0, 0, j * 36));
                    _AttackOver = true;
                    _SR.color = _MyColor;
                }

            }




        }
        protected void Shoot2(int damage, float speed)
        {
            ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                       (() =>
                       {

                           if (_已经死亡) return;
                          
                           int j = 0;
                           //Vector3 dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
                           for (int i = 0; i < 3; i++, j++)//散弹枪
                           {
                               // Debug.LogError(transform.rotation);

                               // Vector3 dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
                               GameObject bullet = Instantiate(子弹, transform.position, Quaternion.identity);
                               BossBullet bossbullet = bullet.GetOrAddComponent<BossBullet>();
                               bossbullet.Damage = damage;
                               bossbullet.speed = speed + 10f;
                               bossbullet.dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;





                           }

                       },
                       0.2f, 3, PETimeUnit.Second);//三到五秒才射击
                                                   //发射子弹
           

        }

    }
}